function [m, gr_m, info] = xt_mean(y,gr,gr_val)
% group mean
% y : variable to be meaned
% gr     : group of y (same length as y)
% gr_val : group values

[nobs, ny] = size(y);

ngr = length(gr_val);

gr_m = zeros(ngr,ny);
gr_n = zeros(ngr,ny);

m = [];
n = [];
for i = 1:1:ngr
    
    temp_y = y(gr==gr_val(i),:);
    temp_n = size(temp_y,1);
    
    loc_m = mean(temp_y,1);
    temp_m = repmat(loc_m,temp_n,1);
    
    % group mean
    m = [m; temp_m];   %obs order
    gr_m(i,:) = loc_m; %gr order
        
    %number of obs
    n = [n; repmat(temp_n,temp_n,1)]; %obs order
    gr_n(i,:) = temp_n; %gr order
    
end

info.n = n;
info.gr_n = gr_n;
